package com.furentech.iotserver.fa.server.handler;

import com.furentech.iotserver.fa.utils.BCD8421Operater;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

public class SinopecFaDecoder extends ByteToMessageDecoder {


    public SinopecFaDecoder(int destiAddr) {
        super();

        this.destiAddr = destiAddr;
        this.expectedCount=0;
        this.tempByteBuf = new byte[4096];
        this.tempByteBufPtr = 0;
        this.bytesExpected=99999999;
    }

    int destiAddr;

    int expectedCount;

    byte[] tempByteBuf;

    int tempByteBufPtr;

    int bytesExpected;

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {

        int thisPackageLength=byteBuf.readableBytes();
        byte[] inbytes = new byte[thisPackageLength];
        byteBuf.readBytes(inbytes);

        for (int i = 0; i < thisPackageLength; i++) {
            if ((inbytes[i] & 0x00ff) == (0xFA & 0x00FF))
            {
                if((inbytes[i + 1] & 0x00ff) == this.destiAddr) {
                    if(this.tempByteBufPtr!=0)
                    {
                        //这里表示上一个完整的帧还在缓存中，需要先发出去

                        //未完待续...
                    }
                    this.tempByteBufPtr=0;
                    this.tempByteBuf[this.tempByteBufPtr++]=inbytes[i];
                    this.tempByteBuf[this.tempByteBufPtr++]=inbytes[i+1];

                    this.bytesExpected=99999999;

                }// if((inbytes[i + 1] & 0x00ff) == this.destiAddr) {
                else{
                    this.tempByteBuf[this.tempByteBufPtr++]=inbytes[i+1];
                    this.bytesExpected-=1;
                    System.out.println(String.format("destiAddr not matched,expected:%02x,received:%02x",this.destiAddr,inbytes[i + 1]));
                }
                i+=1;
            }
            else
            {
                this.tempByteBuf[this.tempByteBufPtr++] = inbytes[i];

                this.bytesExpected-=1;

                //从这里开始表示已经收到了长度位
                if (tempByteBufPtr == 6) {
                    int  validDataLength=0;

                    validDataLength+= BCD8421Operater.bcd2decimal(tempByteBuf[4]);
                    validDataLength*=100;
                    validDataLength+=BCD8421Operater.bcd2decimal(tempByteBuf[5]);

                    this.bytesExpected=validDataLength+2;
                }

                if (this.tempByteBufPtr > 4095) {
                    this.tempByteBufPtr = 0;
                }
            }

            //表示全部收完
            if ((this.bytesExpected == 0)&&(this.tempByteBufPtr>8)) {
                list.add(formNewPackage());
            }

        }// for (int i = 0; i < thisPackageLength; i++)
    }

    private byte[] formNewPackage()
    {
        byte[] newPackageBuf = new byte[this.tempByteBufPtr - 2];
        System.arraycopy(this.tempByteBuf, 2, newPackageBuf, 0, newPackageBuf.length);

        this.tempByteBufPtr=0;
        this.expectedCount=99999999;
        return newPackageBuf;
    }
}
